﻿#ifndef PHYSICS_WORLD
#define PHYSICS_WORLD 1

#include <BulletLibDefine.h>
#include <iostream>
#include <vector>
#include <string>
#include <map>

#include <btBulletDynamicsCommon.h>
#include <osgbCollision/GLDebugDrawer.h>
#include <QUdpSocket>
class BULLET_EDITOR_DLL PhysicsWorld
{
	
public:
	~PhysicsWorld();
	static PhysicsWorld* instance();
	btDiscreteDynamicsWorld* getDynamicsWorld();
	osgbCollision::GLDebugDrawer* getDebugDraw();
	//void addRigidBody( const std::string& name, std::shared_ptr<btRigidBody> rigid_body );
	//void addRigidBody( int id, std::shared_ptr<btRigidBody> rigid_body );//更换了刚体的存储方式后启用，并做了修改
	void addRigidBody( btRigidBody* rigid_body );
    void removeRigidBody( btRigidBody* rigid_body );

	//std::shared_ptr<btRigidBody> getRigidBody(const std::string& name);
	btRigidBody* getRigidBody(int int_id);//通过bullet自身的机制获取。

    const btCollisionObjectArray* getCollisionObjArray();

	void frame();
	void frame(double current_sim_time, double previous_sim_time);


	void setEnableDebugDraw(bool enable_or_disable);
	void setEnableDynamic(bool enable_dynamic);
	void setEnableRealTimeCollisioinDetection(bool enable_real_time_collisioin_detection);
	bool getEnableDebugDraw();
	bool getEnableRealTimeCollisioinDetection();
    void performCollisionOnce();
// 	int getRigidBodyMapSize();
// 	std::string getRigidBodyName(int map_index);

    void*  getUserPointer ();
    void  setUserPointer(void *userPointer);

    QUdpSocket* getSenderSocket();
protected:
	void initPhysicsWorld();

private:
	PhysicsWorld();
	static PhysicsWorld* _instance;
	btDefaultCollisionConfiguration* _collision_configuration;
	btCollisionDispatcher* _collision_dispatcher;
	btConstraintSolver* _constraint_solver;
	btBroadphaseInterface* _broad_phase_interface;
	btDiscreteDynamicsWorld* _discrete_dynamics_world;
	osgbCollision::GLDebugDrawer* _debug_draw;
	bool _enable_debug_draw;
	bool _enable_dynamic;//打开动力学计算，设置为true则计算动力学，否则只有碰撞减测；
	bool _enable_real_time_collisioin_detection;//是否打开实时碰撞检测，关闭的话会提高帧率（在场景复杂的情况下）
    void* m_userObjectPointer;

    QUdpSocket* m_socketSender;
};

#endif //!PHYSICS_WORLD
